﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FinalPointSet.Model.Constant.Message;
using FinalPointSet.Model.Constant;
using FinalPointSet.Model.Constants;
using FinalPointSet.Model.Manager;
using FinalPointSet.Model.Model.Knowledge;
using FinalPointSet.Model.Model.MockData;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using static FinalPointSet.Model.Manager.MessageManager;
using FinalPointSet.Model.Model;
namespace FinalPointSet.ViewModel.Page.MockData
{
    public partial class MockFieldMappingRuleSettingPageViewModel : ObservableObject
    {
        /// <summary>
        /// 规则映射集合
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<MockFieldMappingRule> _MockFieldMappingRules;
        /// <summary>
        /// 规则映射
        /// </summary>
        [ObservableProperty]
        private MockFieldMappingRule? _MockFieldMappingRule;
        /// <summary>
        /// 映射类型
        /// </summary>
        [ObservableProperty]
        private List<MappingMatchType> _MappingMatchTypes = [];
        /// <summary>
        /// 数据类型
        /// </summary>
        [ObservableProperty]
        private List<ClassfiyType> _ClassfiyTypes = [];
        /// <summary>
        /// 规则
        /// </summary>
        [ObservableProperty]
        private List<MockRule> _MockRules = [];
        public MockFieldMappingRuleSettingPageViewModel()
        {
            MockFieldMappingRules = new(AssetManager.Instance.GetJsonObjectList<MockFieldMappingRule>().OrderBy(mfmr => mfmr.Sequence));
            MockRules = AssetManager.Instance.GetJsonObjectList<MockRule>();
            MappingMatchTypes = new List<MappingMatchType>(System.Enum.GetValues(typeof(MappingMatchType)).Cast<MappingMatchType>());
            ClassfiyTypes = new List<ClassfiyType>(System.Enum.GetValues(typeof(ClassfiyType)).Cast<ClassfiyType>());
        }

        #region 命令
        [RelayCommand]
        private void Add()
        {
            Debug.Print("MockFieldMappingRuleSettingPageViewModel.Add()");
            int maxID = (MockFieldMappingRules.Count > 0) ? MockFieldMappingRules.Max(e => e.ID) + 1 : 1;
            int maxSeq = (MockFieldMappingRules.Count > 0) ? MockFieldMappingRules.Max(e => e.Sequence) + 1 : 1;
            MockFieldMappingRule mfmr = new()
            {
                ID = maxID,
                Name = $"规则映射{MockFieldMappingRules.Count + 1}",
                MappingMatchType = MappingMatchType.Keyword,
                ClassfiyType = ClassfiyType.String,
                RuleID = MockRules.Count > 0 ? MockRules[0].ID : 1001,
                Sequence = maxSeq,
            };
            MockFieldMappingRules.Add(mfmr);
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"新增规则映射[{mfmr.Name}]成功!"));
        }

        /// <summary>
        /// 移除规则映射
        /// </summary>
        [RelayCommand]
        private void Delete(MockFieldMappingRule mfmr)
        {
            MockFieldMappingRules.Remove(mfmr);
            Debug.Print($"MockFieldMappingRuleSettingPageViewModel.Delete(), 移除规则映射[{mfmr.Name}]成功!");
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"移除规则映射[{mfmr.Name}]成功!"));
        }

        /// <summary>
        /// 保存
        /// </summary>
        [RelayCommand]
        private void Save()
        {
            if (MockFieldMappingRules == null)
            {
                Debug.Print("MockFieldMappingRuleSettingPageViewModel.Save(),规则映射集合为空");
                return;
            }

            AssetManager.Instance.UpdateJsonObjectList(MockFieldMappingRules.ToList());
            AssetManager.Instance.SaveJsonObjectList<MockFieldMappingRule>();
            Debug.Print($"MockFieldMappingRuleSettingPageViewModel.Save(),保存{MockFieldMappingRules.Count}条数据成功!");
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"保存{MockFieldMappingRules.Count}条数据成功!"));
        }

        /// <summary>
        /// 上移规则映射
        /// </summary>
        [RelayCommand]
        private void MoveUp(MockFieldMappingRule mfmr)
        {
            if (mfmr == null) return;
            int index = MockFieldMappingRules.IndexOf(mfmr);
            if (index <= 0)
            {
                Debug.Print($"MockFieldMappingRuleSettingPageViewModel.MoveUp(), 规则映射已位于最顶部, 无法移动");
                Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Warning, "规则映射已位于最顶部, 无法移动"));
                return;
            }
            //交换排序索引
            (MockFieldMappingRules[index].Sequence, MockFieldMappingRules[index - 1].Sequence) = (MockFieldMappingRules[index - 1].Sequence, MockFieldMappingRules[index].Sequence);
            MockFieldMappingRules = new(MockFieldMappingRules.ToList().OrderBy(mfmr => mfmr.Sequence));
            Debug.Print($"MockFieldMappingRuleSettingPageViewModel.MoveUp(), 上移规则映射:{mfmr.Name}");
        }

        /// <summary>
        /// 下移规则映射
        /// </summary>
        [RelayCommand]
        private void MoveDown(MockFieldMappingRule mfmr)
        {
            if (mfmr == null) return;
            int index = MockFieldMappingRules.IndexOf(mfmr);
            if (index >= MockFieldMappingRules.Count - 1)
            {
                Debug.Print($"MockFieldMappingRuleSettingPageViewModel.MoveDown(), 规则映射已位于最底部, 无法移动");
                Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Warning, "规则映射已位于最底部, 无法移动"));
                return;
            }
            //交换排序索引
            (MockFieldMappingRules[index].Sequence, MockFieldMappingRules[index + 1].Sequence) = (MockFieldMappingRules[index + 1].Sequence, MockFieldMappingRules[index].Sequence);
            MockFieldMappingRules = new(MockFieldMappingRules.ToList().OrderBy(mfmr => mfmr.Sequence));
            Debug.Print($"MockFieldMappingRuleSettingPageViewModel.MoveDown(), 下移规则映射:{mfmr.Name}");
        }
        #endregion
    }
}
